package com.example.mypigcar.controller;

/**
 * @author zkw
 * @Description 订单接口
 * @createTime 2021年07月07日 08:14:00
 */

import com.example.mypigcar.common.core.SR;
import com.example.mypigcar.common.exception.InsufficientBalance;
import com.example.mypigcar.common.exception.OrderExpiredException;
import com.example.mypigcar.common.exception.RepeatPaymentException;
import com.example.mypigcar.pojo.dto.AddOrderDTO;
import com.example.mypigcar.pojo.dto.EndOrderDTO;
import com.example.mypigcar.pojo.dto.PayOrderDTO;
import com.example.mypigcar.pojo.dto.TOrderDTO;
import com.example.mypigcar.pojo.po.TOrder;
import com.example.mypigcar.pojo.vo.AdminOrderListVO;
import com.example.mypigcar.pojo.vo.DetailOrderVO;
import com.example.mypigcar.service.TOrderService;
import com.example.mypigcar.utils.JWTUtils;
import com.example.mypigcar.utils.OSSUtils;
import com.example.mypigcar.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

@Api("订单接口")
@RequestMapping("order")
@RestController
public class OrderController {
    @Resource
    TOrderService orderService;

    @ApiOperation("用户下订单")
    @PostMapping("")
    public SR insertOrder(@RequestBody AddOrderDTO order, HttpServletRequest request) {
        String userIdByToken = JWTUtils.getUserIdByToken(request);
        TOrder tOrder = new TOrder();
        BeanUtils.copyProperties(order, tOrder);
        tOrder.setUserId(Integer.parseInt(userIdByToken));
        boolean insert = orderService.insert(tOrder);
        return insert ? SR.ok() : SR.error();
    }

    @ApiOperation("付款订单")
    @PutMapping("")
    public SR payOrder(@RequestBody PayOrderDTO payOrderDTO, HttpServletRequest request) {
        String userIdByToken = JWTUtils.getUserIdByToken(request);
        try {
            boolean result = orderService.payOrder(payOrderDTO.getOrderId(), Integer.parseInt(userIdByToken));
            return result ? SR.ok() : SR.error().message("系统出错了，请重试");
        } catch (InsufficientBalance | OrderExpiredException | RepeatPaymentException e) {
            e.printStackTrace();
            return SR.error().message(e.getMessage());
        }
    }

    @ApiOperation("查询用户全部订单")
    @GetMapping("")
    public SR getUserOrder(HttpServletRequest request) {
        String userIdByToken = JWTUtils.getUserIdByToken(request);
        try{
            List<TOrder> userAllOrder = orderService.getUserAllOrder(Integer.parseInt(userIdByToken));
            Collections.reverse(userAllOrder);
            return SR.ok().data(userAllOrder);
        }catch (NullPointerException e){
            return SR.error().message("这个用户没有订单噢");
        }

    }

    @ApiOperation("上传还车图片")
    @PostMapping("/upload")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "文件", dataType = "file")})
    public SR uploadImg(MultipartFile file) {
        try {
            System.out.println(file);
            String url = OSSUtils.uploadFile(UUIDUtils.getUUID().toString(), "return_car_img/", file);
            return SR.ok().data(url);
        } catch (Exception e) {
            e.printStackTrace();
            return SR.error();
        }
    }


    @ApiOperation("结束订单")
    @PutMapping("/end")
    public SR EndOrder(@RequestBody @Validated EndOrderDTO endOrderDTO, HttpServletRequest request) {
        System.out.println(endOrderDTO);
        TOrder tOrder = orderService.endOrder(endOrderDTO);
        return SR.ok().data(tOrder);
    }


    //-----------------------------管理员-----------------------------

    @ApiOperation("查询订单信息（管理员）")
    @GetMapping("/admin/list")
    public SR getOrderList(){
        List<AdminOrderListVO> adminOrderListVOS = orderService.selectAdminOrderList();
        return SR.ok().data(adminOrderListVOS);
    }

    @ApiOperation("查询订单还车图片和地点")
    @GetMapping("/admin/{order_id}")
    public SR getDetailOrder(@PathVariable Integer order_id){
        DetailOrderVO detailOrder = orderService.getDetailOrder(order_id);
        return SR.ok().data(detailOrder);
    }
}
